package test;

import cn.hutool.extra.qrcode.BufferedImageLuminanceSource;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import jdk.internal.org.objectweb.asm.Handle;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: App
 * @Author: hhxx_liuzhiguo
 * @CreateTime: 2023-11-10  10:44
 */
public class App {
    public static void main(String[] args) {
        //使用自定义工具包生成二维码
//        File myUtilFileDir = new File("E:\\files");
//        MyQrCodeUtil.createCodeToFile("123",myUtilFileDir,"qrcode1.png");

        //使用hutool工具包生成二维码
        String path = "E:\\files\\" + System.currentTimeMillis() + ".png";
        File hutoolFile = new File(path);
        QrConfig qrConfig = QrConfig.create();
        qrConfig.setBackColor(Color.WHITE);
        qrConfig.setForeColor(Color.BLACK);

        // 附带内容
        Map<String, Object> map = new HashMap<>();
        map.put("name", "张三");
        map.put("age", 25);
        map.put("deptId", 108611);
        map.put("money", 12.56);

        QrCodeUtil.generate(map.toString(), qrConfig, hutoolFile);
        System.out.println("二维码图片生成成功: " + path);

        // 开始读取刚生成的二维码中的内容
        MultiFormatReader multiFormatReader = new MultiFormatReader();
        HashMap hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "GBK");
        try {
            BufferedImage source = ImageIO.read(new File(path));
            BinaryBitmap binaryImg = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(source)));
            Result result = multiFormatReader.decode(binaryImg, hints);
            System.out.println(result.getText());
        } catch (Exception e) {
            System.out.println("二维码内容读取失败：" + e.getMessage());
        }
    }
}
